package com.mobisystems.youpark;

import com.mobisystems.youpark.utils.UtilityFunctions;
import com.parse.LogInCallback;
import com.parse.ParseException;
import com.parse.ParseUser;

import android.app.Fragment;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SignInFragment extends Fragment implements View.OnClickListener{
	private Button signInButton;
	private EditText etUserName;
	private EditText etPassword;
	private UtilityFunctions utilities = new UtilityFunctions();
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// Inflate the layout for this fragment
		View view = inflater.inflate(R.layout.fragment_sign_in, container, false);
		Typeface font = Typeface.createFromAsset( getActivity().getAssets(), "fontawesome-webfont.ttf" );
		
		((Button)view.findViewById(R.id.signInButton)).setTypeface(font);
		
		signInButton = (Button)view.findViewById(R.id.signInButton);
		etUserName = (EditText)view.findViewById(R.id.etUsernameSignIn);
		etPassword = (EditText)view.findViewById(R.id.etPasswordSignIn);
		signInButton.setOnClickListener(this);
		
		
		return view;
	}
	
	@Override
	public void onClick(View v) {
		attemptLogin();
		
	}
	
	private void dispatch() {
		if (ParseUser.getCurrentUser() != null) {
			Log.d("ParseUser", ParseUser.getCurrentUser().getEmail());
			startActivity(new Intent(getActivity(), MyMap.class));
			getActivity().finish();
		} else {
			startActivity(new Intent(getActivity(), MainActivity.class));
			getActivity().finish();
		}
	}
	
	private void attemptLogin() {
		boolean validationError = false;
        StringBuilder validationErrorMessage =
            new StringBuilder(getResources().getString(R.string.error_intro));
        if (utilities.isEmpty(etUserName)) {
          validationError = true;
          validationErrorMessage.append(getResources().getString(R.string.error_blank_username));
        }
        if (utilities.isEmpty(etPassword)) {
          if (validationError) {
            validationErrorMessage.append(getResources().getString(R.string.error_join));
          }
          validationError = true;
          validationErrorMessage.append(getResources().getString(R.string.error_blank_password));
        }
        validationErrorMessage.append(getResources().getString(R.string.error_end));

        // If there is a validation error, display the error
        if (validationError) {
          Toast.makeText(getActivity(), validationErrorMessage.toString(), Toast.LENGTH_LONG)
              .show();
          return;
        }

        // Set up a progress dialog
        final ProgressDialog dlg = new ProgressDialog(getActivity());
        dlg.setTitle("Please wait.");
        dlg.setMessage("Logging in.  Please wait.");
        dlg.show();
        
        String username = etUserName.getText().toString();
		String password = etPassword.getText().toString();
		
		ParseUser.logInInBackground(username, password, new LogInCallback() {
			
			@Override
			public void done(ParseUser user, ParseException e) {
				dlg.dismiss();
				if(e != null) {
					Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_LONG).show();
				} else {
					dispatch();
				}
			}
		});
	}
}
